<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\Support;

use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Description of Redirect
 *
 * @author getssl
 */
class Redirect extends RedirectResponse {

    /**
     * 
     * @param type $url
     * @param type $status
     * @param type $headers
     * @return \Lg\Support\Redirect
     */
    public static function to($url, $status = 302, $headers = []) {

        if (is_string($url)) {
            $redirect_url = url_for($url);
        } else if (is_array($url)) {
            $redirect_url = url_for(Arr::get($url, 0), Arr::get($url, 1));
        }
        return new Redirect($redirect_url, $status, $headers);
    }

    public function with($key, $value = null) {
        $key = is_array($key) ? $key : [$key => $value];
        foreach ($key as $k => $v) {
            \Lg\Session\Session::instance()->addFlash($k, $v);
        }
        return $this;
    }

    public function withCookies(array $cookies) {
        foreach ($cookies as $cookie) {
            $this->headers->setCookie($cookie);
        }
        return $this;
    }

    public function withCookie($cookie) {
        $this->headers->setCookie($cookie);
        return $this;
    }

    public function withInput(array $input = null) {
        $input = $input ? : \Lg\Http\Input::all();
        \Lg\Session\Session::instance()->addFlash('_forward.input', $input);
        return $this;
    }

    public function withFlash($name, $message) {
        \Lg\Session\Session::instance()->addFlash($name, $message);
        return $this;
    }

    /**
     * 
     * @param string $key
     * @param array|string $values
     * @param boolean $replace
     */
    public function header($key, $values, $replace = true) {
        $this->headers->set($key, $values, $replace);
        return $this;
    }

    public function maybe_redirect($query_name = 'redirect') {
        $redirect_url = \Lg\Http\Input::get($query_name,
                        \Lg\Http\Session::getPreviousUrl());
        if (!empty($redirect_url)) {
            $this->setTargetUrl($redirect_url);
        }
        return $this;
    }

    public function __call($method, $parameters) {
        if (Str::startsWith($method, 'with')) {
            return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
        }
        throw new \RuntimeException("Method [$method] does not exist on Redirect.");
    }

}
